package com.itheima.book.feign;

import com.itheima.apis.book.IBookClient;
import com.itheima.book.service.BookAdminService;
import com.itheima.book.service.BookService;
import com.itheima.model.book.dtos.BookAdminDto;
import com.itheima.model.book.dtos.BookSaveDto;
import com.itheima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * BookClient 实现类
 * 这个类作为IBookClient接口在book-service内部的实现
 * 同时也提供REST接口供Feign远程调用
 */
@RestController
public class BookClient implements IBookClient {
    
    @Autowired
    private BookService bookService;
    
    @Autowired
    private BookAdminService bookAdminService;

    /**
     * 获取图书信息
     */
    @Override
    @GetMapping("/api/v1/book/vo")
    public ResponseResult getBookVo(@RequestParam Integer bookId) {
        return bookService.getBookVo(bookId);
    }

    /**
     * 根据分类ID获取图书列表
     */
    @Override
    @GetMapping("/api/v1/book/category")
    public ResponseResult getBooksByCategory(@RequestParam Integer categoryId,
                                             @RequestParam Integer page,
                                             @RequestParam Integer size) {
        return bookService.getBooksByCategory(categoryId, page, size);
    }

    /**
     * 获取新书列表
     */
    @Override
    @GetMapping("/api/v1/book/new")
    public ResponseResult getNewBooks(@RequestParam Integer page,
                                      @RequestParam Integer size) {
        return bookService.getNewBooks(page, size);
    }

    // ==================== 管理端接口实现 ====================

    /**
     * 分页查询图书列表（管理端）
     */
    @Override
    @PostMapping("/api/v1/book/admin/list")
    public ResponseResult adminListBooks(@RequestBody BookAdminDto dto) {
        return bookAdminService.listBooks(dto);
    }

    /**
     * 获取图书详情（管理端）
     */
    @Override
    @GetMapping("/api/v1/book/admin/{id}")
    public ResponseResult adminGetBookById(@PathVariable("id") Integer id) {
        return bookAdminService.getBookById(id);
    }

    /**
     * 新增图书（管理端）
     */
    @Override
    @PostMapping("/api/v1/book/admin")
    public ResponseResult adminAddBook(@RequestBody BookSaveDto dto) {
        return bookAdminService.addBook(dto);
    }

    /**
     * 更新图书（管理端）
     */
    @Override
    @PutMapping("/api/v1/book/admin/{id}")
    public ResponseResult adminUpdateBook(@PathVariable("id") Integer id, @RequestBody BookSaveDto dto) {
        dto.setId(id);
        return bookAdminService.updateBook(dto);
    }

    /**
     * 删除图书（管理端）
     */
    @Override
    @DeleteMapping("/api/v1/book/admin/{id}")
    public ResponseResult adminDeleteBook(@PathVariable("id") Integer id) {
        return bookAdminService.deleteBook(id);
    }

    /**
     * 批量删除图书（管理端）
     */
    @Override
    @PostMapping("/api/v1/book/admin/batch-delete")
    public ResponseResult adminBatchDeleteBooks(@RequestBody List<Integer> ids) {
        return bookAdminService.batchDeleteBooks(ids);
    }
}

